import axios from 'axios';
import base from './config';
// const baseURL = 'http://localhost:8080/api';
const baseURL= `${base}/api`
export const LoginService = async (userData) => {
    try {
        const response = await axios.post(`${baseURL}/admin/login`, userData);
        return response.data;
    } catch (error) {
        console.error('Error during login:', error);
        throw error;
    }
}

export  const fetchAdminInfoService = async () => {
    try {
        const response = await axios.get(`${baseURL}/admin/info`);
        return response.data;
    } catch (error) {
        console.error('Error fetching admin info:', error);
        throw error;
    }
}

export const listUsersService = async (params) => {
    try {
        const response = await axios.get(`${baseURL}/admin/user/list`, {
            params: params
        });
        return response.data;
    } catch (error) {
        console.error('Error fetching user list:', error);
        throw error;
    }
}

export const userDetailService = async (userId) => {
    try {
        const response = await axios.get(`${baseURL}/admin/user/detail/${userId}`);
        return response.data;
    } catch (error) {
        console.error('Error fetching user detail:', error);
        throw error;
    }
}

export const updateUserExpService = async (userId, expData) => {
    try {
        const response = await axios.put(`${baseURL}/admin/user/experience/${userId}`, null, {
            params: expData
        });
        return response.data;
    } catch (error) {
        console.error('Error updating user experience:', error);
        throw error;
    }
}

export const updateUserBTService = async (userId, btData) => {
    try {
        const response = await axios.put(`${baseURL}/admin/user/bt/${userId}`, null, {
            params: btData
        });
        return response.data;
    } catch (error) {
        console.error('Error updating user BT:', error);
        throw error;
    }
}

export const banUserService = async (userId) => {
    try {
        const response = await axios.put(`${baseURL}/admin/user/ban/${userId}`);
        return response.data;
    } catch (error) {
        console.error('Error banning user:', error);
        throw error;
    }
}

export const unbanUserService = async (userId) => {
    try {
        const response = await axios.put(`${baseURL}/admin/user/unban/${userId}`);
        return response.data;
    } catch (error) {
        console.error('Error unbanning user:', error);
        throw error;
    }
}

export const updateUserGroupService = async (userId, group) => {
    try {
        const response = await axios.put(`${baseURL}/admin/user/permission/${userId}`, null, {
            params: group
        });
        return response.data;
    } catch (error) {
        console.error('Error updating user group:', error);
        throw error;
    }
}

export const listReportsService = async (params) => {
    try {
        const response = await axios.get(`${baseURL}/report/list`, {
            params: params
        });
        return response.data;
    } catch (error) {
        console.error('Error fetching report list:', error);
        throw error;
    }
}

export const updateReportService = async (params) => {
    try {
        const response = await axios.post(`${baseURL}/report/process`, null, {
            params: params
        });
        return response.data;
    } catch (error) {
        console.error('Error updating report:', error);
        throw error;
    }
}

// 考核管理相关API
export const listAssessmentRulesService = async (params) => {
    try {
        const response = await axios.get(`${baseURL}/assessment/admin/rules`, {
            params: params
        });
        return response.data;
    } catch (error) {
        console.error('Error fetching assessment rules:', error);
        throw error;
    }
}

export const createAssessmentRuleService = async (ruleData) => {
    try {
        const response = await axios.post(`${baseURL}/assessment/admin/rule`, ruleData);
        return response.data;
    } catch (error) {
        console.error('Error creating assessment rule:', error);
        throw error;
    }
}

export const updateAssessmentRuleService = async (ruleData) => {
    try {
        const response = await axios.put(`${baseURL}/assessment/admin/rule`, ruleData);
        return response.data;
    } catch (error) {
        console.error('Error updating assessment rule:', error);
        throw error;
    }
}

export const deleteAssessmentRuleService = async (ruleId) => {
    try {
        const response = await axios.delete(`${baseURL}/assessment/admin/rule/${ruleId}`);
        return response.data;
    } catch (error) {
        console.error('Error deleting assessment rule:', error);
        throw error;
    }
}

export const toggleRuleStatusService = async (ruleId, isActive) => {
    try {
        const response = await axios.put(`${baseURL}/assessment/admin/rule/${ruleId}/toggle`, null, {
            params: { isActive }
        });
        return response.data;
    } catch (error) {
        console.error('Error toggling rule status:', error);
        throw error;
    }
}

export const getAssessmentStatisticsService = async (params) => {
    try {
        const response = await axios.get(`${baseURL}/assessment/admin/statistics`, {
            params: params
        });
        return response.data;
    } catch (error) {
        console.error('Error fetching assessment statistics:', error);
        throw error;
    }
}

export const executeManualAssessmentService = async (assessmentData) => {
    try {
        const response = await axios.post(`${baseURL}/assessment/admin/execute`, assessmentData);
        return response.data;
    } catch (error) {
        console.error('Error executing manual assessment:', error);
        throw error;
    }
}

export const getMetricTypesService = async () => {
    try {
        const response = await axios.get(`${baseURL}/assessment/metric-types`);
        return response.data;
    } catch (error) {
        console.error('Error fetching metric types:', error);
        throw error;
    }
}

export const getPenaltyTypesService = async () => {
    try {
        const response = await axios.get(`${baseURL}/assessment/penalty-types`);
        return response.data;
    } catch (error) {
        console.error('Error fetching penalty types:', error);
        throw error;
    }
}